package com.tj.collection.controller.admin;

import com.fqgj.common.api.annotations.ParamsValidate;
import com.fqgj.common.utils.CollectionUtils;
import com.tj.collection.bean.ApiResponse;
import com.tj.collection.bean.SystemConstants;
import com.tj.collection.common.RequestLocalInfo;
import com.tj.collection.controller.admin.request.AddMenuVo;
import com.tj.collection.controller.admin.request.UpdateMenuVo;
import com.tj.collection.controller.admin.response.MenuListResponse;
import com.tj.collection.controller.admin.response.RoleMenuIdsResponse;
import com.tj.collection.controller.admin.response.UserMenuResponse;
import com.tj.collection.db.domain.FormedMenuVO;
import com.tj.collection.db.domain.Menu;
import com.tj.collection.db.service.AdminMenuService;
import com.tj.collection.db.service.AdminPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/3/13
 * Time: 下午3:41
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private AdminMenuService adminMenuService;

    @Autowired
    private AdminPermissionService adminPermissionService;

    /**
     * 获取登录用户拥有的菜单权限
     *
     * @return
     */
    @RequestMapping("/user")
    public ApiResponse<UserMenuResponse> adminMenuList() {
        List<Long> menuIds = adminMenuService.getMenuIdsByAdmin(RequestLocalInfo.getCurrentAdmin().getId());
        List<Menu> menuList = adminMenuService.getMenusByIds(menuIds);
//        this.menuFilter(menuList);
        return new ApiResponse<>(new UserMenuResponse(new FormedMenuVO(menuList)));
    }

    /**
     * 根据parentId获取子菜单
     *
     * @param parentId
     * @return
     */
    @RequestMapping("/parent/{parentId}/list")
    public ApiResponse<MenuListResponse> getListByParent(@PathVariable Long parentId) {
        List<Menu> menuList = adminMenuService.getAllMenusByParentId(parentId);
        this.menuFilter(menuList);
        return new ApiResponse<>(new MenuListResponse(menuList));
    }

    /**
     * 添加菜单
     *
     * @param addMenuVo
     * @return
     */
    @RequestMapping("/add")
    @ParamsValidate
    public ApiResponse add(@RequestBody AddMenuVo addMenuVo) {
        adminMenuService.add(addMenuVo.tran2Menu());
        return new ApiResponse<>();
    }

    /**
     * 更新菜单
     *
     * @param id
     * @param updateMenuVo
     * @return
     */
    @RequestMapping("/{id}/update")
    @ParamsValidate
    public ApiResponse update(@PathVariable Long id, @RequestBody UpdateMenuVo updateMenuVo) {
        adminMenuService.update(updateMenuVo.tran2Menu().setMenuId(id));
        return new ApiResponse<>();
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @RequestMapping("/{id}/delete")
    @ParamsValidate
    public ApiResponse delete(@PathVariable Long id) {
        adminMenuService.delete(id);
        return new ApiResponse<>();
    }

    /**
     * 获取所有的菜单
     *
     * @return
     */
    @RequestMapping("/list")
    public ApiResponse allMenus() {
        List<Menu> menuList = adminMenuService.getAllMenus();
        this.menuFilter(menuList);
        return new ApiResponse<>(new UserMenuResponse(new FormedMenuVO(menuList)));
    }

    /**
     * 根据角色获取菜单ID
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/role/{roleId}/ids")
    public ApiResponse roleMenuIds(@PathVariable Long roleId) {
        List<Long> roleMenuIds = adminMenuService.getMenuIdsByRole(roleId);
        this.menuIdFilter(roleMenuIds);
        return new ApiResponse<>(new RoleMenuIdsResponse(roleMenuIds));
    }

    private void menuFilter(List<Menu> menuList) {
        if (CollectionUtils.isEmpty(menuList)) {
            return;
        }
        String merchantCode = RequestLocalInfo.getCurrentAdmin().getMerchantCode();
        if (!SystemConstants.MEBOO.equals(merchantCode)) {
            Iterator<Menu> iterator = menuList.iterator();
            while (iterator.hasNext()) {
                Menu menu = iterator.next();
                if (Arrays.asList(2, 3, 4, 12).contains(menu.getMenuId().intValue())) {
                    iterator.remove();//使用迭代器的删除方法删除
                }
            }
        }

    }

    private void menuIdFilter(List<Long> roleMenuIds) {
        if (CollectionUtils.isEmpty(roleMenuIds)) {
            return;
        }
        String merchantCode = RequestLocalInfo.getCurrentAdmin().getMerchantCode();
        if (!SystemConstants.MEBOO.equals(merchantCode)) {
            Iterator<Long> iterator = roleMenuIds.iterator();
            while (iterator.hasNext()) {
                Long menuId = iterator.next();
                if (Arrays.asList(2, 3, 4, 12).contains(menuId.intValue())) {
                    iterator.remove();//使用迭代器的删除方法删除
                }
            }
        }
    }

//    @RequestMapping("/operation/list")
//    public ApiResponse allMenusWithOperations() {
//        MenuWithOperationsVO menuWithOperationsVO = adminPermissionService.getAllPermissionAndMenuIdMap();
//        return new ApiResponse<>(new UserMenuWithOperationsResponse(menuWithOperationsVO));
//    }
}
